//
//  LLTodayActionViewController.swift
//  LiLi
//
//  Created by liuzhu on 2019/1/24.
//  Copyright © 2019 liuzhu. All rights reserved.
//

import UIKit

class LLTodayActionViewController: UIViewController, UITextFieldDelegate {
    public var day: LLDay? {
        didSet {
            actions = day?.actions
        }
    }

    public var actions: [LLAction]? {
        didSet {
            tableView.reloadData()
        }
    }

    var dataPicker: EWDatePickerViewController = EWDatePickerViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(tableView)
        tableView.register(LLActionCell.self, forCellReuseIdentifier: LLActionListIdentifier) // 注册cell
        navigationController?.navigationBar.tintColor = UIColor.darkGray
        title = "当天日程"
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.tabBarController?.tabBar.isHidden = true
    }

    private lazy var tableView: UITableView = {
        var y = CGFloat(64)
        if LLConst.getPhoneType() == PhoneType.PHONE_MAX_XR || LLConst.getPhoneType() == PhoneType.PHONE_X {
            y = 88
        }
        let t = UITableView(frame: CGRect(x: 0, y: y, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - 135))
        t.delegate = self
        t.dataSource = self
        t.separatorStyle = .none
        return t
    }()
}

extension LLTodayActionViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let count = actions?.count ?? 0
        return count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: LLActionListIdentifier, for: indexPath) as! LLActionCell
        let action = actions![indexPath.row]
        cell.action = action
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 73
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let action = actions![indexPath.row]
        let alertVC = UIAlertController(title: nil, message: nil
                                        , preferredStyle: UIAlertController.Style.actionSheet)
        let actionRemark = UIAlertAction(title: "日记", style: UIAlertAction.Style.default) { _ in
            let vc = LLRemarkViewController()
            vc.action = action
            self.navigationController?.pushViewController(vc, animated: false)
        }
        let actionSet = UIAlertAction(title: "设置 ", style: UIAlertAction.Style.default) { _ in

            self.dataPicker.dateStyle = DatePickerStyle.DATEPICKERSTYLE_ACTIONSET
            self.dataPicker.actionSetView.nameContentTF.delegate = self
            self.dataPicker.actionSetView.desContentTF.delegate = self
            self.dataPicker.actionSetView.nameContentTF.text = action.titleName
            self.dataPicker.actionSetView.desContentTF.text = action.des
            self.dataPicker.backActionInfo = { [weak self] name, des, _, _, _ in
                action.titleName = name
                action.des = des
                action.refreshTag = action.refreshTag! + 1 //本地更新action
                LLManager.shareLLManager().saveAction(action: action, finish: { isSuccess in
                    if isSuccess {
                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: LLActionUpdateKey), object: nil, userInfo: ["action":action])
                        self?.navigationController?.popViewController(animated: false)
                    }
                })
            }
            self.dataPicker.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
            /// 弹出时日期滚动到当前日期效果
            self.present(self.dataPicker, animated: true) {
            }
        }
        let actionDelete = UIAlertAction(title: "删除", style: UIAlertAction.Style.default) { _ in
            let alertVC = UIAlertController(title: "提示", message: "你确定邵删除日程[\(action.titleName ?? "")]么", preferredStyle: UIAlertController.Style.alert)
            let actionSure = UIAlertAction(title: "确定", style: UIAlertAction.Style.default, handler: { (_) in
                let action = self.actions![indexPath.row]
                LLActionManager.shareActionManager().deleteAction(action: action, finish: { _ in
                    self.navigationController?.popViewController(animated: false)
                })
            })
            let actionCancel = UIAlertAction(title: "取消 ", style: UIAlertAction.Style.cancel, handler: { (_) in
                
            })
            alertVC.addAction(actionSure)
            alertVC.addAction(actionCancel)
            self.present(alertVC, animated: true, completion: nil)
        }
        
        let actionCancel = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel) { _ in
        }

        alertVC.addAction(actionRemark)
        alertVC.addAction(actionSet)
        alertVC.addAction(actionDelete)
        alertVC.addAction(actionCancel)
        present(alertVC, animated: true, completion: nil)
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        animateViewMoving(up: true, moveValue: 200)
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        animateViewMoving(up: false, moveValue: 200)
    }

    func animateViewMoving(up: Bool, moveValue: CGFloat) {
        let movementDuration: TimeInterval = 0.3
        let movement: CGFloat = (up ? -moveValue : moveValue)
        UIView.beginAnimations("animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration)
        dataPicker.view.frame = dataPicker.view.frame.offsetBy(dx: 0, dy: movement)
        UIView.commitAnimations()
    }
}
